package com.yango.teach.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yango.serviceutil.exception.YangoException;
import com.yango.teach.entity.Subject;
import com.yango.teach.entity.vo.subject.ExcelSubjectData;
import com.yango.teach.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * @author Li.Ding.yin
 * @create 2021年07月24日2:00-星期六
 * @role
 */
public class SubjectExcelListener extends AnalysisEventListener<ExcelSubjectData> {

    //注入
    @Autowired
    private SubjectService subjectService;

    public SubjectExcelListener() {//无参构造
    }

    //创建有参数构造，传递subjectService用于操作数据库
    public SubjectExcelListener(SubjectService subjectService) {//有参构造
        this.subjectService = subjectService;
    }

    //一行一行读取
    @Override
    public void invoke(ExcelSubjectData subject, AnalysisContext analysisContext) {
        //判断excel文件是否为空
        if (subject == null) {
            throw new YangoException(20001, "文件内容为空,导入失败!");
        }
        //添加一级分类
        Subject existOneSubject = this.existOneSubject(subjectService, subject.getOneSubjectName());
        if (existOneSubject == null) {//没有相同的数据
            existOneSubject = new Subject();//声明一个subject对象
            existOneSubject.setTitle(subject.getOneSubjectName());//设置名称
            existOneSubject.setParentId("0");//设置parent_id为0
            subjectService.save(existOneSubject);//调用方法添加
        }
        //获取一级分类id
        String id = existOneSubject.getId();
        //添加二级分类
        Subject existTwoSubject = this.existTwoSubject(subjectService, subject.getTwoSubjectName(), id);
        if (existTwoSubject == null) {//没有相同的
            existTwoSubject = new Subject();//声明一个subject对象
            existTwoSubject.setTitle(subject.getTwoSubjectName());//设置名称
            existTwoSubject.setParentId(id);//设置parent_id为0
            subjectService.save(existTwoSubject);//调用方法添加
        }
    }

    //读取表头信息
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println(headMap);
    }

    //判断一级分类是否重复-根据parent_id=0判断
    private Subject existOneSubject(SubjectService subjectService, String name) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", "0");
        wrapper.eq("title", name);
        Subject oneSubject = subjectService.getOne(wrapper);
        return oneSubject;
    }

    //判断二级分类是否重复-根据parent_id=pid判断
    private Subject existTwoSubject(SubjectService subjectService, String name, String pid) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", pid);
        wrapper.eq("title", name);
        Subject twoSubject = subjectService.getOne(wrapper);
        return twoSubject;
    }

    //读取完成后执行
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}
